package com.conorroche.taskmanagement;

import com.google.android.gms.maps.*;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class LocationActivity extends Activity
	implements ActionBar.TabListener, OnMapClickListener, OnInfoWindowClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location);
		
		// Set up the action bar.
		// Set up the action bar to show tabs.
		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
				
		// For each of the sections in the app, add a tab to the action bar.
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tasks)
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tags)
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_location)
				.setTabListener(this));

		actionBar.setSelectedNavigationItem(2);

		FragmentManager myFragmentManager = getFragmentManager();
		MapFragment myMapFragment = (MapFragment) myFragmentManager.findFragmentById(R.id.map);
		myMap = myMapFragment.getMap();

		myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		myMap.setMyLocationEnabled(true);
		myMap.getUiSettings().setCompassEnabled(true);
		myMap.getUiSettings().setAllGesturesEnabled(true);
		myMap.getUiSettings().setZoomControlsEnabled(true);

		
		db = new DBAdapter(getApplicationContext());
		db.open();
		Cursor tasks = db.getAllTasks();
		tasks.moveToFirst();
		while(!tasks.isAfterLast()){
			String name = tasks.getString(tasks.getColumnIndex("name"));
			double lng = tasks.getDouble(tasks.getColumnIndex("longitude"));
			double lat = tasks.getDouble(tasks.getColumnIndex("latitude"));
			String date = tasks.getString(tasks.getColumnIndex("date"));
			String time = tasks.getString(tasks.getColumnIndex("time"));
			myMap.addMarker(new MarkerOptions()
				.position(new LatLng(lat, lng))
				.title(name)
				.snippet("This task is due by: " + date + " at: " + time));
			tasks.moveToNext();
		}
		db.close();
		
		myMap.setOnMapClickListener(this);
		myMap.setOnInfoWindowClickListener(this);
	}
	@Override
	 protected void onResume() {
		super.onResume();
		actionBar.setSelectedNavigationItem(2);
		int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
		if (resultCode != ConnectionResult.SUCCESS){
			GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
		}
	}
	@Override
	public void onMapClick(LatLng point) {
		Log.d("Location Activity", point.toString());
		myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
	}
	@Override
	public void onInfoWindowClick(Marker marker) {
		Intent viewTask = new Intent(getApplicationContext(), ViewTaskActivity.class);
		viewTask.putExtra("name", marker.getTitle());
		startActivity(viewTask);
	 }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.location, menu);
		return true;
	}
	@Override
	public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
		switch(tab.getPosition()){
			case 0:	// all tasks
				if (first){
					first = false;
				}else{
					Intent allTasks = new Intent(getApplicationContext(), MainActivity.class);
					startActivity(allTasks);
				}
			break;
			case 1: //all tags
				Intent tagsIntent = new Intent(getApplicationContext(), TagsActivity.class);
				startActivity(tagsIntent);
			break;
			case 2://Locations

			break;
		}
	}
	@Override
	public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
	}
	@Override
	public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.add_task:
        		addTask = new Intent(getApplicationContext(), AddTask.class);
            	addTask.putExtra("edit", false);
        		startActivity(addTask);
            	return true;
        	case R.id.add_tag:
        		addTag = new Intent(getApplicationContext(), AddTag.class);
        		startActivity(addTag);
        		return true;
        	case R.id.menu_legalnotices:
        		String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext());
        	    AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(LocationActivity.this);
        	    LicenseDialog.setTitle("Legal Notices");
        	    LicenseDialog.setMessage(LicenseInfo);
        	    LicenseDialog.show();
        	    return true;
        }
        return super.onOptionsItemSelected(item);
        
	}

	private final int RQS_GooglePlayServices = 1;
	private GoogleMap myMap;
	private Intent addTask, addTag;
	private boolean first = true;
	private ActionBar actionBar;
	DBAdapter db;
}